package com.attendance.spring;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

/**
 * spring mvc 工具类
 *
 * @author 鵺丶
 * @date 2021/03/12
 */
@Component
public class SpringSecurityUtils {

    private SpringSecurityUtils(){}

    /**
     * 获得当前用户名
     *
     * @return {@link String}
     */
    public static String getUserName() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof User){
            return ((User) principal).getUsername();
        }else{
            return "Anonymous User";
        }
    }
}
